package com.ropeok.controller;

import com.ropeok.Result.Result;
import com.ropeok.entity.User;
import com.ropeok.entity.permissions;
import com.ropeok.mapper.PermissionMapper;
import com.ropeok.service.serviceimpl.ServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "操作权限表")
@RestController
public class PermissionsController {
    @Autowired
    PermissionMapper permissionMapper;
    @Autowired
    ServiceImpl service;


    @ApiOperation(value = "查权限",notes = "查所有的权限")
    @GetMapping(value = "/findper")
    public Result findper(){
        List<permissions> list = permissionMapper.FindAll();
        return Result.success(list);
    }

    @ApiOperation(value = "增加权限信息",notes = "增加权限信息")
    @PostMapping(value = "/saveper")
    public Result saveper(@Valid permissions permissions, BindingResult bindingResult ){
        if (bindingResult.hasErrors()) {
            return Result.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        service.SavePermission(permissions);
        return Result.success();
    }


    @ApiOperation(value = "删除某个权限",notes = "通过权限id删除权限")
    @GetMapping(value = "/delpers{id}")
    public Result delpermission( Integer id){
        Result result;
        if (null==id){
            result=Result.error("id不能为空");
        }else{
            service.DelPermission(id);
            result=Result.success();
        }
        return result;
    }
}
